import { createRouter, createWebHistory } from 'vue-router'

// 懒加载组件
const Home = () => import('../views/Home.vue')
const Login = () => import('../views/Login.vue')
const ProductList = () => import('../views/product/ProductList.vue')
const ProductForm = () => import('../views/product/ProductForm.vue')
const OrderList = () => import('../views/order/OrderList.vue')
const OrderDetail = () => import('../views/order/OrderDetail.vue')
const PaymentList = () => import('../views/payment/PaymentList.vue')
const PaymentReconciliation = () => import('../views/payment/PaymentReconciliation.vue')
const PaymentReport = () => import('../views/payment/PaymentReport.vue')
const NotFound = () => import('../views/NotFound.vue')

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      requiresAuth: false,
      title: '登录'
    }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('../components/Layout.vue'),
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '',
        name: 'Home',
        component: Home,
        meta: {
          title: '首页'
        }
      },
      {
        path: '/product/list',
        name: 'ProductList',
        component: ProductList,
        meta: {
          title: '商品列表'
        }
      },
      {
        path: '/product/add',
        name: 'ProductAdd',
        component: ProductForm,
        meta: {
          title: '添加商品'
        }
      },
      {
        path: '/product/edit/:id',
        name: 'ProductEdit',
        component: ProductForm,
        meta: {
          title: '编辑商品'
        }
      },
      {
        path: '/order/list',
        name: 'OrderList',
        component: OrderList,
        meta: {
          title: '订单列表'
        }
      },
      {
        path: '/order/detail/:id',
        name: 'OrderDetail',
        component: OrderDetail,
        meta: {
          title: '订单详情'
        }
      },
      {
        path: '/payment/list',
        name: 'PaymentList',
        component: PaymentList,
        meta: {
          title: '支付记录'
        }
      },
      {
        path: '/payment/reconciliation',
        name: 'PaymentReconciliation',
        component: PaymentReconciliation,
        meta: {
          title: '对账管理'
        }
      },
      {
        path: '/payment/report',
        name: 'PaymentReport',
        component: PaymentReport,
        meta: {
          title: '支付报表'
        }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound,
    meta: {
      requiresAuth: false,
      title: '页面不存在'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || 'AI Shop 管理后台'
  
  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      next({ name: 'Login' })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router